<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        //从左到右  如果a>b a+1   a<=b 不变 给右序处理
        //从右到左  如果a>b a+1   a<=b 不变  给左序处理
        (function (ratings) {
            let candyArr = new Array(ratings.length).fill(1);
            for(let i =0;i<ratings.length-1;i++){
                if(ratings[i]>ratings[i+1]){
                    candyArr[i] +=1;
                }
            }
            let right = 0;
            for(let i =ratings.length-1;i>=0;i--){
                if(ratings[i]>ratings[i-1]){
                   right ++;
                }else{
                    right =1
                }
                candyArr[i] = Math.max(right,candyArr[i]);
            }
         console.log(candyArr)
         return candyArr.reduce((a,b)=>a+b,0)
        })([1,2,87,87,87,2,1])
    </script>

</body>

</html>